package fr.persistence.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fr.form.Application;
import fr.persistence.ManageApplicationPersistenceService;
import fr.utilities.Constantes.StatutApplication;

@Service
public class ManageApplicationPersistenceServiceImpl 
	extends GenericDaoImpl<Application, Integer> 
	implements ManageApplicationPersistenceService{

    @Autowired
	private SessionFactory sessionFactory;

	public ManageApplicationPersistenceServiceImpl() {
		super(Application.class);
	}

	public ManageApplicationPersistenceServiceImpl(Class<Application> type) {
		super(type);
	}
	
	@SuppressWarnings("unchecked")
	public List<Application> getStartedApplications() {
    	Criteria crit = sessionFactory.getCurrentSession().createCriteria(Application.class);
    	
    	List<Integer> listeStatut = new ArrayList<Integer>();
    	listeStatut.add(StatutApplication.DISPONIBLE.getCode());
    	listeStatut.add(StatutApplication.EPUISEE.getCode());
		
    	crit.add(Restrictions.in("statutApplication",listeStatut));
    	
    	return (List<Application>)crit.list();

	}

	@SuppressWarnings("unchecked")
	public List<Application> readByStatut(Integer statutRecherche) {
    	Criteria crit = sessionFactory.getCurrentSession().createCriteria(Application.class);
    	if(0 != statutRecherche){
        	crit.add(Restrictions.eq("statutApplication",statutRecherche));
    	}
    	return (List<Application>)crit.list();
	}
	

}
